#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:          catalogView.py
# Author:      KuKei
# Create Date: 2012-01-09
# Description: 参数管理视图
# Modify Date: 2012-01-09

from sd.cms.views.admin import AdminBaseView
from sd.cms.lib import Params, Result
from sd.cms.forms.paramForm import ParamForm
from sd.cms.models.paramModel import Param, Column
from sd.cms.lib.decorator import powerLevel

class ParamView(AdminBaseView):
    @powerLevel(Params.INITIATOR)
    def add(self, **args):
        paramForm = ParamForm()
        if self.request.POST:
            paramForm = ParamForm(self)
            if paramForm.is_valid():
                formData = paramForm.cleaned_data
                paramModel = Param().initData(formData)
                paramModel.save()
                self.mc.flushNS2(self.params.PARAM)
                self.result = Result(True,'操作成功！','','/admin/list!Param/'+str(formData['kind'])+'.html',2)
            else:
                self.result = Result(False,paramForm.GetErrors(),'','',3)
        return self.render_json()
        
    @powerLevel(Params.INITIATOR)
    def edit(self, **args):
        paramForm = ParamForm()
        if self.request.POST:
            paramForm = ParamForm(self)
            if paramForm.is_valid():
                formData = paramForm.cleaned_data
                objId = int(self.request.POST['_id'])
                if objId > -1:
                    paramModel = Param().initData(formData)
                    paramModel.id = objId
                    paramModel.save()
                    self.mc.flushNS2(self.params.PARAM)
                    self.result = Result(True,'操作成功！','','/admin/list!Param/'+str(formData['kind'])+'.html',2)
                else:
                    self.result = Result(False,paramForm.GetErrors(),'','/admin/list!Param/'+str(formData['kind'])+'.html',3)
            else:
                self.result = Result(False,paramForm.GetErrors(),'','',3)
        if self.request.POST.has_key('_retType') and self.request.POST['_retType'] == 'json':
            return self.render_json()
        else:
            self.viewData["result"] = self.result
            return self.render('/snippet/skip.html')
    
    @powerLevel(Params.INITIATOR)
    def delete(self, **args):
        if self.request.POST:
            if Column.objects.filter(model=int(args['id'])).count() > 0:
                self.result.flag = False
                self.result.msg = "存在使用此模型的栏目，不能删除！"
            else:
                paramModel = Param.objects.get(id=int(args['id']))
                paramModel.delete()
                self.mc.flushNS2(self.params.PARAM)
                self.result.flag = True
                self.result.msg = "删除成功！"
        return self.render_json()
        
    def listView(self, **args):
        paramModels = Param.objects.filter(kind=int(args['kind'])).order_by('orderby')
        paramList = []
        for param in paramModels:
            myParamForm = ParamForm(self,modelData=param)
            paramList.append(myParamForm)
        paramForm = ParamForm()
        paramForm.fields['kind'].initial = int(args['kind'])
        self.viewData['paramList'] = paramList
        self.viewData['paramForm'] = paramForm
        return self.render('/admin/listParam.html')
    
    def detailView(self, **args):
        param = Param.objects.get(id=int(args['id']))
        paramForm = ParamForm(self,modelData=param)
        self.viewData["etype"] = 'param'
        self.viewData["e"] = paramForm
        return self.render('/admin/include/detail.html')
    
    def toOptions(self, **args):
        retList = [("","---------")]
        paramList = Param.objects.filter(kind=int(args['kind'])).order_by('orderby')
        for param in paramList:
            retList.append(eval('('+str(param.id)+',"'+param.name+'")'))
        return retList
        
    
def action(request, **args):
    return apply(ParamView(request).action, (), args)